function [status,msg] = addNewServer(uri,sName,msgBox)
% ADDNEWSERVER adds a new DAS service for browsing within the browser
% 
% ADDNEWSERVER interacts with the DAS Registry and updates a local structure
% that stores all DAS services available for browsing in DASMiner GUI.
% 
% SYNTAX: [status] = addNewServer(uri)
%          uri - URI identifier of the DAS service, provided by the DAS Registry
%          status - 1 (success), 0 (failure).
%          
% EXAMPLE:
%     create service for Uniprot
%     status = addNewServer('DS_409');
%     
% See also POPULATESERVERMENU, SEARCHREGISTRY_GUI
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, March 2008.


status = 1;
[xmlS, xml,status] = searchDASRegistry('URI',uri, 'timeout', 5000);

    if (~checkDASResponse(xmlS,status))
        return;
    end

    if (isfield(xmlS.SOURCE{1}.VERSION{1},'CAPABILITY')) %there should be at least one capability

        try
            strServer = load('localServ.mat', 'locServ');
            locServ = strServer.locServ;
            
            %check if it is already in
            for i=1:size(locServ,2)
                if (strcmp(locServ(i).uri,uri)) 
                    status = 0;
                    msg = [ sName ':  DAS service is already available for local browsing.'] ;
                    if (msgBox)
                        msgbox(msg ,'DAS Registry Discovery','warn');
                    end
                    return;
                end;
            end
            
        catch
            locServ = struct;
        end
        locServ = add2Servers(locServ,xmlS);
        save localServ.mat locServ;
        msg = [sName ': new DAS service was added to the local browser.'];
        if (msgBox)
            msgbox(msg,'DAS Registry Discovery','warn');
        end
    else
        status = 0;
        msg = [sName ': DAS service has no described capabilities. Service cannot be added.'];
        if (msgbox)
            msgbox( msg ,'DAS Registry Discovery','warn');
        end
        return;
    end

function [locServ] = add2Servers(locServ, xmlS)

if (isempty(fields(locServ)))
    index = 1;
else
    index = size(locServ,2) + 1;
end
    
locServ(index).uri = xmlS.SOURCE{1}.ATTRIBUTE.uri;
locServ(index).title = xmlS.SOURCE{1}.ATTRIBUTE.title;
locServ(index).desc = xmlS.SOURCE{1}.ATTRIBUTE.description;

%Extracting baseURL and dsn from the query_uri. eg.
%http://www.ensembl.org/das/Aedes_aegypti.AaegL1.transcript/features
locServ(index).baseUrl = regexp(xmlS.SOURCE{1}.VERSION{1}.CAPABILITY{1}.ATTRIBUTE.query_uri,'[\w\W]*/das/','match');
locServ(index).baseUrl = locServ(index).baseUrl{1};
dsnTokens = regexp(xmlS.SOURCE{1}.VERSION{1}.CAPABILITY{1}.ATTRIBUTE.query_uri,'das/[\w\W]*','match');
%eg. dsnTokens = 'das/Aedes_aegypti.AaegL1.transcript/features'
tokens = regexp(dsnTokens,'/','split');
locServ(index).dsn = tokens{1}{2};


%Get capability info
%Get the first capability
tokens = regexp(xmlS.SOURCE{1}.VERSION{1}.CAPABILITY{1}.ATTRIBUTE.type,':','split');
capabilities = tokens{2};

for i=2:size(xmlS.SOURCE{1}.VERSION{1}.CAPABILITY,2)
    tokens = regexp(xmlS.SOURCE{1}.VERSION{1}.CAPABILITY{i}.ATTRIBUTE.type,':','split');
    capabilities = strcat(capabilities,',',tokens{2});
end

if (isempty(regexp(capabilities,',','split'))) %1 capability only
    locServ(index).capabilities = cellstr(capabilities);
else
    locServ(index).capabilities = cellstr(regexp(capabilities,',','split'));
end

%Get coordinates info
%Get first coordinate
cs = xmlS.SOURCE{1}.VERSION{1}.COORDINATES{1}.ATTRIBUTE.source;
csURI = xmlS.SOURCE{1}.VERSION{1}.COORDINATES{1}.ATTRIBUTE.uri;

for i=2:size(xmlS.SOURCE{1}.VERSION{1}.COORDINATES,2)
   cs = strcat(cs,',',xmlS.SOURCE{1}.VERSION{1}.COORDINATES{i}.ATTRIBUTE.source);
   csURI = strcat(csURI,',',xmlS.SOURCE{1}.VERSION{1}.COORDINATES{i}.ATTRIBUTE.uri);
end

if (isempty(regexp(cs,',','split'))) %1 coordinate only
    locServ(index).coordinates = cellstr(cs);
    locServ(index).coordinatesURI = cellstr(csURI);
else
    locServ(index).coordinates = cellstr(regexp(cs,',','split'));
    locServ(index).coordinatesURI = cellstr(regexp(csURI,',','split'));
end



if (isfield(xmlS.SOURCE{1}.VERSION{1},'PROP'))

    for i=1:size(xmlS.SOURCE{1}.VERSION{1}.PROP,2)
        locServ(index).prop(i).name = xmlS.SOURCE{1}.VERSION{1}.PROP{i}.ATTRIBUTE.name;
        locServ(index).prop(i).value = xmlS.SOURCE{1}.VERSION{1}.PROP{i}.ATTRIBUTE.value;
        locServ(index).prop(i).content = xmlS.SOURCE{1}.VERSION{1}.PROP{i}.CONTENT;
    end
end

